package com.helloniu.red5.stream;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.red5.io.flv.impl.FLVReader;
import org.red5.server.net.rtmp.event.AudioData;
import org.red5.server.net.rtmp.event.IRTMPEvent;
import org.red5.server.net.rtmp.event.VideoData;
import org.red5.server.stream.FileStreamSource;

public class LiveFLVData {
    private static ArrayList<IRTMPEvent> vlist = new ArrayList<IRTMPEvent>();
    private static boolean inited = false;
    private static int index = 0;
    
    public static void init() {
        if(inited) {           
            return;
        }
        
        System.out.println("===============play FLV=========");
        
        try {
            FLVReader flvr = new FLVReader(new File("d:/IronMan.flv"));            
            FileStreamSource fss = new FileStreamSource(flvr);
            
            while(fss.hasMore()) {
                IRTMPEvent evt = fss.dequeue();

                if(evt instanceof VideoData) {
                    vlist.add(evt);                   
                }                
                else if(evt instanceof AudioData) {
                    vlist.add(evt);
                }
            }            
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        inited = true;
    }
      
    public static IRTMPEvent getRTMPEvent() {        
        try {
            init();        
        } 
        catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        IRTMPEvent a  = null;
        
        if(index == 0) {
            System.out.println("__________get RTMP event: " + index +"|"+ vlist.size());    
        }        
     
        if(index < vlist.size()) {
            a = vlist.get(index);
            index++;
        }
        else {
            index = 0;
            
            if(vlist.size() > index) {
                a = vlist.get(index);    
            }
        }

        return a;
    }
}
